<template>
  <el-container>
    <el-main style="padding: 20px;">

      <el-card shadow="always" body-style="padding: 0px; min-height: 500px;">
      <el-container>
          <el-header class="topbar">
              <div class="left-panel">
                  <el-button type="success" icon="el-icon-plus" @click="save()">创建新限流规则</el-button>
              </div>
              <div class="right-panel">
                  <div class="right-panel-search">
                      <el-input v-model="searchForm.searchText" placeholder="搜索 规则名称/备注" style="width: 250px;"></el-input>
                      <el-button type="primary" icon="el-icon-search" @click="getList"></el-button>
                      <el-button type="warning" icon="el-icon-refresh" @click="getList"></el-button>
                  </div>
              </div>
          </el-header>

          <el-main>
              <el-table
              :data="pageResult.list"
              element-loading-text="加载数据中"
              v-loading="loading"
              :header-row-style="{height:45+'px'}"
              :header-cell-style="{background:'#F2F6FC'}"
              :row-style="{height:60+'px'}"
              style="width: 100%; min-height: 450px;">
                  <el-table-column prop="flowName"          label="限流规则名" ></el-table-column>
                  <el-table-column label="状态" >
                    <template slot-scope="scope">
                        <el-tag type="success" v-if="scope.row.status">启用</el-tag>
                        <el-tag type="danger" v-else>禁用</el-tag>
                    </template>
                  </el-table-column>
                  <el-table-column prop="remark"  label="备注" ></el-table-column>
                  <el-table-column prop="createTime"  label="创建时间" ></el-table-column>
                  <el-table-column label="操作" width="250">
                      <template slot-scope="scope">
                          <el-button
                              type="text"
                              icon="el-icon-files"
                              style="color: #E6A23C"
                              @click="editFlowConfig(scope.$index, scope.row)">限流配置</el-button>
                          <el-button
                              type="text"
                              icon="el-icon-edit"
                              @click="update(scope.$index, scope.row)">编辑</el-button>
                          <el-button
                              type="text"
                              style="color: #F56C6C"
                              icon="el-icon-delete"
                              @click="deleteInRow(scope.$index, scope.row)">删除</el-button>        
                      </template>
                  </el-table-column>
                  <template #empty>
                      <el-empty :description="'暂无数据'" :image-size="100"></el-empty>
                  </template>
              </el-table>

              <pagination :total="pageResult.total" @page-change="pageChange"></pagination>
          </el-main>

      </el-container>
    </el-card>    
    </el-main>
    
    <save-update-flow ref="SaveUpdateFlow" @success="success"></save-update-flow>
  </el-container>
</template>

<script>
import SaveUpdateFlow from './modules/save_update_flow.vue'

export default {
    components: {
        SaveUpdateFlow
    },
    data() {
        return {
        searchForm: {
            searchText: ''
        },
        pageResult: { 
            total: 0,
            pageNum: 1,
            pageSize: 10,
            list: []
        },
        loading: false
        }
    },
    mounted() {
        // 初始化列表
        this.getList()
    },
    methods: {
        /**
         * @title 获取列表
         */
        async getList() {
            this.loading = true
            try {
                let response = await this.$API.flow.getList(this.searchForm)   
                this.pageResult = response.data
            } finally{
                this.loading = false
            }
        },

        /**
         * @title 更改分页
         */
        pageChange(pageNum, pageSize) {
            this.searchForm.pageNum = pageNum
            this.searchForm.pageSize = pageSize
            this.getList()
        },


        editFlowConfig(index, row) {
            this.$router.push({ path: '/flow/detail', query: { flowId: row.id, flowName: row.flowName }})
        },


        deleteInRow(index, row) {
            this.$confirm('此操作将会删除该限流规则 ['+row.flowName+']，是否继续?', '操作提示', {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning',
                center: true
            }).then(() => {
                
                this.$API.flow.deleteById(row.id).then(response => {
                    this.getList()
                    this.$message.success(response.message)
                })

            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                })
            })
        },

        save() {
            this.$nextTick(() => {
                this.$refs.SaveUpdateFlow.open({'status': true}) 
            })
        },

        update(index, row) {
            this.$nextTick(() => {
                this.$refs.SaveUpdateFlow.open(row) 
            })
        },

        success(id) {
            this.getList()
        }
    }
}
</script>

<style lang="scss" scoped>

.el-main {
    width: 100%;
    height: 100%;
    padding: 0;
}
.topbar {
    height: 50px;
    border-bottom: 1px solid #ebeef5; 
    background: #F2F6FC;
    box-shadow: 0 1px 4px rgba(0,21,41,.08);
    display: flex;
    justify-content:space-between;

    .left-panel {
        display: flex;
        align-items: center;
    }
    .right-panel {
        display: flex;
        align-items: center;
    }

    .right-panel-search {display: flex;align-items: center;}

    .right-panel-search > * + * {margin-left:10px;}
}
</style>

